package misc

import (
	jsoniter "github.com/json-iterator/go"
	"servers/models/define"
)

func VerifyHttpFields(check []string, src map[string]string) bool {
	for _, v := range check {
		if _, ok := src[v]; !ok {
			return false
		}
	}
	return true
}

func VerifyStringLen(str string, size int) bool {
	return len(str) < size
}

func VerifyPlayerCtrlJson(js string) ([][]any, bool) {
	if len(js) <= 0 || len(js) > define.PlayerCtrlJsonLen {
		return nil, false
	}

	var ctrlJs [][]any
	if err := jsoniter.UnmarshalFromString(js, &ctrlJs); err != nil {
		return nil, false
	}
	for _, v := range ctrlJs {
		if len(v) != 2 {
			return nil, false
		}
	}
	return ctrlJs, true
}

func VerifyAutoCtrlJson(js string) bool {
	if len(js) <= 0 || len(js) > define.AutoCtrlJsonLen {
		return false
	}

	var ctrlJs [][]any
	if err := jsoniter.UnmarshalFromString(js, &ctrlJs); err != nil {
		return false
	}
	for _, v := range ctrlJs {
		if len(v) != 3 {
			return false
		}
	}
	return true
}
